package com.haixiaoke.saas.lock.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.haixiaoke.saas.common.constant.LockConstants;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.houseMember.domain.HouseMember;
import com.haixiaoke.saas.lock.domain.HouseLock;
import com.haixiaoke.saas.lock.domain.LockFace;
import com.haixiaoke.saas.lock.mapper.LockFaceMapper;
import com.haixiaoke.saas.lock.service.ILockFaceService;
import com.haixiaoke.saas.lock.service.LockDevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 锁人脸信息Service业务层处理
 *
 * @author qixi
 * @date 2024-07-26
 */
@Service
public class LockFaceServiceImpl implements ILockFaceService {
    @Autowired
    private LockFaceMapper lockFaceMapper;
    @Autowired
    private LockDevicesService lockDevicesService;

    /**
     * 查询锁人脸信息
     *
     * @param faceId 锁人脸信息主键
     * @return 锁人脸信息
     */
    @Override
    public LockFace selectLockFaceByFaceId(Long faceId) {
        return lockFaceMapper.selectLockFaceByFaceId(faceId);
    }

    /**
     * 查询锁人脸信息列表
     *
     * @param lockFace 锁人脸信息
     * @return 锁人脸信息
     */
    @Override
    public List<LockFace> selectLockFaceList(LockFace lockFace) {
        return lockFaceMapper.selectLockFaceList(lockFace);
    }

    /**
     * 新增锁人脸信息
     *
     * @param lockFace 锁人脸信息
     * @return 结果
     */
    @Override
    public int insertLockFace(LockFace lockFace) {
        lockFace.setCreateTime(DateUtils.getNowDate());
        return lockFaceMapper.insertLockFace(lockFace);
    }

    /**
     * 修改锁人脸信息
     *
     * @param lockFace 锁人脸信息
     * @return 结果
     */
    @Override
    public int updateLockFace(LockFace lockFace) {
        lockFace.setUpdateTime(DateUtils.getNowDate());
        return lockFaceMapper.updateLockFace(lockFace);
    }

    /**
     * 批量删除锁人脸信息
     *
     * @param faceIds 需要删除的锁人脸信息主键
     * @return 结果
     */
    @Override
    public int deleteLockFaceByFaceIds(Long[] faceIds) {
        return lockFaceMapper.deleteLockFaceByFaceIds(faceIds);
    }

    /**
     * 删除锁人脸信息信息
     *
     * @param faceId 锁人脸信息主键
     * @return 结果
     */
    @Override
    public int deleteLockFaceByFaceId(Long faceId) {
        return lockFaceMapper.deleteLockFaceByFaceId(faceId);
    }

    @Override
    public List<LockFace> selectLockFaceListByIdentifications(List<String> identifications) {
        return lockFaceMapper.selectLockFaceListByIdentifications(identifications);
    }

    @Override
    public Long[] selectLockFaceListAndLockState(String houseId, Long userId, String lockState) {
        return lockFaceMapper.selectLockFaceListAndLockState(houseId, userId, lockState);
    }

    @Override
    public int insertBatchLockFace(List<LockFace> lockFaceList) {
        return lockFaceMapper.insertBatchLockFace(lockFaceList);
    }

    @Override
    public int deleteLockFaceByLockId(Long lockId) {
        return lockFaceMapper.deleteLockFaceByLockId(lockId);
    }


    @Override
    public List<LockFace> selectLockFaceListAndLockIdAndUserTypeAndFaceState(Long lockId, String userType, String faceState) {
        return lockFaceMapper.selectLockFaceListAndLockIdAndUserTypeAndFaceState(lockId, userType, faceState);
    }

    @Override
    public List<LockFace> selectLockFaceListAndHouseIdAndUserTypeAndFaceState(String houseId, String userType, String faceState) {
        return lockFaceMapper.selectLockFaceListAndHouseIdAndUserTypeAndFaceState(houseId, userType, faceState);
    }

    @Override
    public List<LockFace> selectLockFaceListAndHouseIdAndUserType(String houseId, String userType) {
        return lockFaceMapper.selectLockFaceListAndHouseIdAndUserType(houseId, userType);
    }

    @Override
    public List<LockFace> selectLockFaceByLockIdAndUserType(Long lockId, String userType) {
        return lockFaceMapper.selectLockFaceByLockIdAndUserType(lockId, userType);
    }

    @Override
    public LockFace createLockFace(HouseLock houseLock, HouseMember houseMember, Date nowDate) {
        String format = DateUtil.format( nowDate, "yyyyMMdd");
        String identification = format + RandomUtil.randomNumbers(8);

        LockFace lockFace = new LockFace();
        lockFace.setImg(houseMember.getFacePhoto());
        lockFace.setUserType(LockConstants.USER_TYPE_TENANT);
        lockFace.setLockId(houseLock.getLockId());
        lockFace.setUserId(houseMember.getUserId());
        lockFace.setOrderId(houseMember.getOrderId());
        lockFace.setUserName(houseMember.getMemberName());
        lockFace.setCreateBy(houseMember.getUpdateBy());
        lockFace.setHouseId(houseLock.getHouseId());
        lockFace.setIdentification(identification);
        lockFace.setCreateTime(DateUtils.getNowDate());
        lockDevicesService.createLockFace(lockFace.getImg(), houseLock.getMac(), identification);
        return lockFace;
    }

    @Override
    public int deleteLockFaceByOrderId(String orderId) {
        return lockFaceMapper.deleteLockFaceByOrderId(orderId);
    }

    @Override
    public LockFace selectFaceByLockIdAndUserIdAndUserType(Long lockId, Long userId, String userType) {
        return lockFaceMapper.selectFaceByLockIdAndUserId(lockId, userId, userType);
    }


    @Override
    public List<LockFace> selectLockFaceListAndLockId(Long lockId) {
        return lockFaceMapper.selectLockFaceListAndLockId(lockId);
    }
}
